package com.china.itapi.common.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class AesAlgorithm {
	
	   /**
	    *  aes加密
	     * <p>(适用条件[可选])
	     * <p>(执行流程[可选])
	     * <p>(使用方法[可选])
	     * <p>(注意事项[可选])
	     *
	     * @Description:
	     * @param content  需要加密的内容
	     * @param password  加密密钥
	     * @return byte[]
	     * @Created:yangyuzhou 2013-11-18下午5:10:45
	     * @Modified:
	    */
	private byte[] encrypt(String content, String password) {
		   try {
			   KeyGenerator kgen = KeyGenerator.getInstance("AES");
			   kgen.init(128, new SecureRandom(password.getBytes()));
			   SecretKey secretKey = kgen.generateKey();
			   byte[] enCodeFormat = secretKey.getEncoded();
			   SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			   Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			   byte[] byteContent = content.getBytes("utf-8");
			   cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
			   byte[] result = cipher.doFinal(byteContent);
			   return result; // 加密
			   } catch (NoSuchAlgorithmException e) {
				   e.printStackTrace();
				   } catch (NoSuchPaddingException e) {
					   e.printStackTrace();
					   } catch (InvalidKeyException e) {
						   e.printStackTrace();
						   } catch (UnsupportedEncodingException e) {
							   e.printStackTrace();
							   } catch (IllegalBlockSizeException e) {
								   e.printStackTrace();
								   } catch (BadPaddingException e) {
									   e.printStackTrace();
									   }
		   return null;
		   } 
	   
	   /**
	    * aes解密
	     * <p>(适用条件[可选])
	     * <p>(执行流程[可选])
	     * <p>(使用方法[可选])
	     * <p>(注意事项[可选])
	     *
	     * @Description:
	     * @param content   待解密内容
	     * @param password  解密密钥
	     * @return byte[]
	     * @Created:yangyuzhou 2013-11-19上午10:34:15
	     * @Modified:
	    */
	   private byte[] decrypt(byte[] content, String password) {
		   try {
			   KeyGenerator kgen = KeyGenerator.getInstance("AES");
			   kgen.init(128, new SecureRandom(password.getBytes()));
			   SecretKey secretKey = kgen.generateKey();
			   byte[] enCodeFormat = secretKey.getEncoded();
			   SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			   Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			   cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 
			   byte[] result = cipher.doFinal(content);
			   return result; // 加密
			   } catch (NoSuchAlgorithmException e) {
				   e.printStackTrace();
				   } catch (NoSuchPaddingException e) {
					   e.printStackTrace(); 
					   } catch (InvalidKeyException e) {
						   e.printStackTrace();
						   } catch (IllegalBlockSizeException e) {
							   e.printStackTrace();
							   } catch (BadPaddingException e) {
								   e.printStackTrace();
								   } 
		   return null; 
		   }  
	   
	   
	   /**
	    *  byte数组转base64
	     * <p>(适用条件[可选])
	     * <p>(执行流程[可选])
	     * <p>(使用方法[可选])
	     * <p>(注意事项[可选])
	     *
	     * @Description:
	     * @param encryptedByteArray
	     * @return String
	     * @Created:yangyuzhou 2013-11-18下午5:10:20
	     * @Modified:
	    */
	   private String byteConvertToBase(byte[] encryptedByteArray)
	   {
		   BASE64Encoder enc = new BASE64Encoder();
		   String encryptedtext = enc.encode(encryptedByteArray);
		   return encryptedtext;	   
	   }
	   /**
	    * 
	     * <p>(适用条件[可选])
	     * <p>(执行流程[可选])
	     * <p>(使用方法[可选])
	     * <p>(注意事项[可选])
	     *
	     * @Description:BASE64转byte数组
	     * @param text
	     * @return byte[]
	     * @Created:yangyuzhou 2013-11-28下午5:38:45
	     * @Modified:
	    */
	   private byte[] baseConvertTobyte(String text)
	   {
		   byte[] result = null;
		   try {
			   result = new sun.misc.BASE64Decoder().decodeBuffer(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
		   
	   }
	   
	   
	   /**
	    * 
	     * <p>(适用条件[可选])
	     * <p>(执行流程[可选])
	     * <p>(使用方法[可选])
	     * <p>(注意事项[可选])
	     *
	     * @Description:加密
	     * @param content
	     * @param password
	     * @return String
	     * @Created:yangyuzhou 2013-11-28下午5:47:30
	     * @Modified:
	    */
	   public String encryptToBase64(String content, String password){
		   String result = "";
		   try{
			   result = byteConvertToBase(encrypt(content,password));
		   }catch(Exception e){
			   e.printStackTrace();
		   }		   
		   return result;		   
	   }
	   
	   /**
	    * 
	     * <p>(适用条件[可选])
	     * <p>(执行流程[可选])
	     * <p>(使用方法[可选])
	     * <p>(注意事项[可选])
	     *
	     * @Description:解密
	     * @param content
	     * @param password
	     * @return String
	     * @Created:yangyuzhou 2013-11-28下午5:47:36
	     * @Modified:
	    */
	   public String decryptToString(String content, String password){
		   
		   String result = "";
		   try{
			   result = new String (decrypt(baseConvertTobyte(content),password));
		   }catch(Exception e){
			   e.printStackTrace();
		   }		   		   		   
		   return result;		   
	   }

}
